package ru.ifmo.itmokis.domain;

import java.io.Serializable;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 *
 * @author Ivan Stepuk
 */
@Entity(name = "Order1")
@Table(name = "Orders")
public class Order implements Serializable {

    private int id;
    private User customer;
    private Type type;
    private Status status;
    private Time approvementTime;
    private Time deadline;
    private Time completionTime;
    private int totalProducts;
    private int completedProducts;

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    protected void setId(int id) {
        this.id = id;
    }

    @ManyToOne
    @JoinColumn(name = "customer", referencedColumnName = "id")
    public User getCustomer() {
        return customer;
    }

    public void setCustomer(User customer) {
        this.customer = customer;
    }

    @Enumerated(value = EnumType.STRING)
    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    @Enumerated(value = EnumType.STRING)
    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    @Embedded
    @AttributeOverride(name = "value", column =
    @Column(name = "approvementTime"))
    public Time getApprovementTime() {
        return approvementTime;
    }

    public void setApprovementTime(Time approvementTime) {
        this.approvementTime = approvementTime;
    }

    @Embedded
    @AttributeOverride(name = "value", column =
    @Column(name = "deadline"))
    public Time getDeadline() {
        return deadline;
    }

    public void setDeadline(Time deadline) {
        this.deadline = deadline;
    }

    @Embedded
    @AttributeOverride(name = "value", column =
    @Column(name = "completionTime"))
    public Time getCompletionTime() {
        return completionTime;
    }

    public void setCompletionTime(Time completionTime) {
        this.completionTime = completionTime;
    }

    public int getTotalProducts() {
        return totalProducts;
    }

    public void setTotalProducts(int totalProducts) {
        this.totalProducts = totalProducts;
    }

    public int getCompletedProducts() {
        return completedProducts;
    }

    public void setCompletedProducts(int completedProducts) {
        this.completedProducts = completedProducts;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Order other = (Order) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + this.id;
        return hash;
    }

    @Override
    public String toString() {
        return "Order №" + id + " deadline: " + deadline
                + " (" + completedProducts + '/' + totalProducts + ')';
    }

    public enum Type {

        STANDARD(1),
        URGENT(2);
        private final int value;

        private Type(int value) {
            this.value = value;
        }
    }

    public enum Status {

        APPROVED(1),
        IN_PROGRESS(2),
        COMPLETED(3);
        private final int value;

        private Status(int value) {
            this.value = value;
        }
    }
}
